package de.wittekind.bundesjugendspiele.client.management.ui.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.TreePath;

import de.wittekind.bundesjugendspiele.client.management.conf.Constants;
import de.wittekind.bundesjugendspiele.client.management.ui.UIController;
import de.wittekind.bundesjugendspiele.server.interfaces.Riege;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler;

public class MainView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6445905213113852472L;

	UIController ui;

	JPanel left;
	JPanel right;
	RiegenView r;
	DetailView dv;

	MainActionListener m ;

	public MainView(UIController ui) {
		super();
		this.ui = ui;
		m = new MainActionListener(ui, this);
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e)

		{
			e.printStackTrace();
			return;
		}

		setJMenuBar(createMenu());

		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);

		JMenuBar menuBar = new JMenuBar();

		left = new JPanel();
		r = new RiegenView(ui);
		r.addTreeSelectionListener(m);
		r.addActionListener(m);
		JScrollPane scroll = new JScrollPane(r);
		left.setLayout(new GridBagLayout());
		GridBagConstraints g = new GridBagConstraints();
		g.fill = GridBagConstraints.BOTH;
		g.weightx = 1;
		g.weighty = 1;
		left.add(scroll, g);

		right = new JPanel();
		right.add(new JPanel());
		right.setLayout(new GridBagLayout());

		if (de.wittekind.bundesjugendspiele.client.management.conf.Constants.DEBUG) {
			right.setOpaque(true);
			right.setBackground(Color.CYAN);
			left.setOpaque(true);
			left.setBackground(Color.ORANGE);
		}

		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.EAST;
		c.weightx = 0.2;
		c.weighty = 0.5;
		add(left, c);

		c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.gridy = 0;
		c.weightx = 1.0;
		c.weighty = 0.5;
		c.anchor = GridBagConstraints.WEST;
		add(right, c);
		setSize(new Dimension(900, 450));

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				System.exit(0);
			}
		});
	}

	private JMenuBar createMenu() {
		JMenuBar menuBar = new JMenuBar();
		JMenu file = new JMenu(Constants.msg.getString("file"));
		JMenuItem createRiege = new JMenuItem(
				Constants.msg.getString("newRiege"));
		createRiege.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
				ActionEvent.CTRL_MASK));

		createRiege.addActionListener(m);
		createRiege.setName("newRiege");
		
		
		file.add(createRiege);

		JMenuItem createSchueler = new JMenuItem(
				Constants.msg.getString("newSchueler"));
		createSchueler.setName("newSchueler");
		createSchueler.addActionListener(m);
		file.add(createSchueler);

		file.addSeparator();

		JMenuItem saveCurrent = new JMenuItem(Constants.msg.getString("save"));
		saveCurrent.setName("save");
		saveCurrent.addActionListener(m);
		saveCurrent.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
				ActionEvent.CTRL_MASK));

		file.add(saveCurrent);

		file.addSeparator();

		JMenu importMenu = new JMenu(Constants.msg.getString("import"));

		JMenuItem importSchuelerList = new JMenuItem(
				Constants.msg.getString("importSchueler"));
		importSchuelerList.setName("import");
		importSchuelerList.addActionListener(m);
		importMenu.add(importSchuelerList);

		file.add(importMenu);

		JMenu edit = new JMenu(Constants.msg.getString("edit"));

		menuBar.add(file);
		menuBar.add(edit);

		JProgressBar b = new JProgressBar();
		b.setIndeterminate(true);
		b.setSize(100, 10);

		menuBar.add(b);
		
		return menuBar;
	}

	public boolean showError(String msg) {
		return showError(msg, true);
	}

	public boolean showError(String msg, boolean retryable) {
		Object[] options = { "erneut versuchen", "abbrechen" };
		int i = JOptionPane.showOptionDialog(null, msg, "Error",
				JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null,
				options, options[0]);
		return i == JOptionPane.YES_OPTION;

	}

	public boolean displayNode(BujuNode b) {
		try {
			right.removeAll();
			GridBagConstraints g = new GridBagConstraints();
			g.fill = GridBagConstraints.BOTH;
			g.weightx = 1;
			g.weighty = 1;
			if (b.getSchueler() != null) {
				dv = new SchuelerDetailView(b.getSchueler(), ui);
			} else {
				dv = new RiegeDetailView(b.getRiege(), ui);
			}
			right.add(dv, g);
			right.updateUI();
			ui.setActive(b.getSchueler(), b.getRiege());
			dv.addActionListener(m);
		} catch (IOException ex) {
			return !showError(Constants.msg.getString("serverNotConnected"));
		}

		return true;
	}

	public void addBujuNode(BujuNode b, boolean selected, boolean visible) {

		r.addBujuNode(b, selected);
		if (visible | selected) {
			TreePath path = new TreePath(b.getPath());
			r.makeVisible(path);

			if (selected) {
				this.r.setSelectionPath(path);
				this.r.updateUI();
			}
		}
	}
	

	public DetailView getDetailView() {
		return dv;
	}
	


	public void refresh() throws IOException {
		r.refresh();
		r.updateUI();
	}

	public void removeSchueler(Riege r, Schueler s) {
		this.r.remove(r, s);
	}
	
	public void removeRiege(Riege r) {
		this.r.remove(r);
	}

}
